package com.fyakm.daodejing.simple;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class EvolutionController {
    // 定义遗传的相关规则和操作，比如基因交叉的方式、概率等，这里先简单示意
    private double crossoverRate;
    // 定义变异的相关规则和操作，例如变异概率、变异影响程度等
    private double mutationRate;

    public EvolutionController(double crossoverRate, double mutationRate) {
        this.crossoverRate = crossoverRate;
        this.mutationRate = mutationRate;
    }

    // 这个方法接收一个物种，然后根据遗传和变异规则返回进化后的物种
    public Species evolveSpecies(Species species) {
        Species offspring = performGeneticInheritance(species);
        performMutation(offspring);
        return offspring;
    }

//    // 执行遗传操作的方法，这里简单模拟基因交叉（假设物种的基因用一个简单的数组表示）
//    private Species performGeneticInheritance(Species parent) {
//        // 这里假设找另一个随机的亲本进行交叉繁殖，实际应用可能需要更合理的配对逻辑
//        Species anotherParent = findAnotherParent();
//        int[] parentGenes = parent.getGenes();
//        int[] anotherParentGenes = anotherParent.getGenes();
//        int[] childGenes = new int[parentGenes.length];
//        // 简单的交叉逻辑示例，以中间为交叉点
//        int crossoverPoint = parentGenes.length / 2;
//        for (int i = 0; i < crossoverPoint; i++) {
//            childGenes[i] = parentGenes[i];
//        }
//        for (int i = crossoverPoint; i < anotherParentGenes.length; i++) {
//            childGenes[i] = anotherParentGenes[i];
//        }
//        return new Species(childGenes);
//    }

    // 执行变异操作的方法，调用物种类自身的变异方法并传入变异概率
    private void performMutation(Species species) {
        species.mutate(mutationRate);
    }

    // 简单的找另一个亲本的方法示例，实际需要完善逻辑保证合理配对等
    private Species findAnotherParent() {
        // 假设这里有一个全局的物种集合可以从中选取，实际应用中要结合具体场景调整
        return SpeciesPool.getRandomSpecies();
    }



    private int crossoverPointsCount; // 新增交叉点数属性

    public EvolutionController(double crossoverRate, double mutationRate, int crossoverPointsCount) {
        this.crossoverRate = crossoverRate;
        this.mutationRate = mutationRate;
        this.crossoverPointsCount = crossoverPointsCount;
    }

    private Species performGeneticInheritance(Species parent) {
        Species anotherParent = findAnotherParent();
        int[] parentGenes = parent.getGenes();
        int[] anotherParentGenes = anotherParent.getGenes();
        int[] childGenes = new int[parentGenes.length];

        // 生成多个交叉点并排序
        List<Integer> crossoverPoints = generateCrossoverPoints(parentGenes.length);

        int currentParentIndex = 0;
        for (int i = 0; i < parentGenes.length; i++) {
            if (crossoverPoints.contains(i)) {
                currentParentIndex = (currentParentIndex + 1) % 2; // 切换亲本
            }
            if (currentParentIndex == 0) {
                childGenes[i] = parentGenes[i];
            } else {
                childGenes[i] = anotherParentGenes[i];
            }
        }

        return new Species(childGenes);
    }

    private List<Integer> generateCrossoverPoints(int geneLength) {
        List<Integer> points = new ArrayList<>();
        Random random = new Random();
        for (int i = 0; i < crossoverPointsCount; i++) {
            int point = random.nextInt(geneLength);
            points.add(point);
        }
        points.sort(Integer::compareTo);
        return points;
    }
}